<?php
/**
 * This class correspond to the class [http://code.google.com/apis/opensocial/docs/0.7/reference/gadgets.Tab.html gadgets.Tab] from the OpenSocial API, with the difference that this one has more specific information.
 * @package    gadgets
 * @subpackage feature
 * @author     Daniel Botelho <botelho.daniel@gmail.com>
 */
class GadgetsTab
{
  
  private $fetch_mode;
  
  private $var;
  private $params_var;
  
  private $contentContainer;
  private $callback;
  private $tooltip;
  private $index;
  private $name;
  
  /**
   * By default the name corresponds to the contentContainer.
   *
   * @param string $name
   * @param boolean $fetch_mode If true this means that this variable was fetched by the container
   */
  public function __construct($name,$fetch_mode=false)
  {
    if(!$fetch_mode){
      OSConfig::importTabs();
      $this->name = $name;
      $this->params_var = StringUtil::normalizeString($name).'_params';
      $this->setContentContainerElement($name);
    }else
      $this->var = $name;
    
    $this->fetch_mode = $fetch_mode;
  }
  
  /**
   * Throws sfException if not in Fetch Mode
   * @throws sfException If not in Fetch Mode
   */
  private function assertFetchMode()
  {
    if(!$this->fetch_mode)
      throw new sfException("You can only use this method when in fetch_mode");
  }
  
  /**
   * Throws sfException if in Fetch Mode
   * @throws sfException If in Fetch Mode
   */
  private function assertNotFetchMode()
  {
    if($this->fetch_mode)
      throw new sfException('You can not use this method in fetch_mode');
  }
  
  public function getVarName()
  {
    $this->assertFetchMode();
    return $this->var; 
  }
  
  /**
   * Returns the defined name of this Tab
   * @return string Returns the defined name of this Tab
   */
  public function getName()
  { 
    $this->assertNotFetchMode();
    return $this->name;
  }
  
  /**
   * The name of the varible that holds the params
   *
   * @return string The name of the varible that holds the params
   */
  public function getParamsVar()
  {
    $this->assertNotFetchMode();
    return $this->params_var;
  }
  
  /**
   * Returns the parameters of this Tab
   * @return string The params
   */
  public function getParams()
  {
    $this->assertNotFetchMode();
    $params_var= $this->getParamsVar();
    $html = StringUtil::appendableString("var $params_var = {}",false);
    if($this->contentContainer)
      $html.= StringUtil::appendableString($params_var.'.contentContainer = '.$this->contentContainer,false);
    if($this->callback)
      $html.= StringUtil::appendableString($params_var.'.callback = '.$this->callback->getName(),false);
    if($this->tooltip)
      $html.= StringUtil::appendableString($params_var.'.tooltip = \''.$this->tooltip.'\'',false);
    if($this->index)
      $html.= StringUtil::appendableString($params_var.'.index = '.$this->index,false);
    
    return StringUtil::newLine($html);
  }
  
  public function setContentContainerElement($elementId)
  {
    $this->assertNotFetchMode();
  	$this->contentContainer = "document.getElementById('$elementId')";
  }
  
  public function setContentContainer($contentContainer)
  {
    $this->assertNotFetchMode();
  	$this->contentContainer = $contentContainer;
  }
  public function setCallback(JSFunction $callback)
  {
    $this->assertNotFetchMode();
  	$this->callback = $callback;
  }
  public function setTooltip($tooltip)
  {
    $this->assertNotFetchMode();
  	$this->tooltip = $tooltip;
  }
  public function setIndex($index)
  {
    $this->assertNotFetchMode();
    if(!is_integer($index))
      throw new sfException("index must be integer. You have given '$index'");
  	$this->index = $index;
  }
  /**
   * Returns the HTML element where the tab content is rendered. 
   * @return Element Returns the HTML element where the tab content is rendered. 
   */
  public function getTabContentContainer($appendable=false)
  {
    $this->assertFetchMode();
  	return StringUtil::appendableString($this->getVarName().'.getContentContainer()',$appendable);
  }
  /**
   * Returns the callback function that is executed when the tab is selected.
   *
   * @return Function Returns the callback function that is executed when the tab is selected.
   */
  public function getTabCallback($appendable=false)
  {
    $this->assertFetchMode();
  	return StringUtil::appendableString($this->getVarName().'.getCallback()',$appendable);
  }
  /**
   * Returns the label of the tab as a string (may contain HTML). 
   *
   * @return String Returns the label of the tab as a string (may contain HTML). 
   */
  public function getTabName($appendable=false)
  {
    $this->assertFetchMode();
  	return StringUtil::appendableString($this->getVarName().'.getName()',$appendable);
  }
  /**
   * Returns the tab's index. 
   *
   * @return Number Returns the tab's index. 
   */
  public function getTabIndex($appendable=false)
  {
    $this->assertFetchMode();
  	return StringUtil::appendableString($this->getVarName().'.getIndex()',$appendable);
  }
  /**
   * Returns the HTML element that contains the tab's label.
   *
   * @return Element Returns the HTML element that contains the tab's label.
   */
  public function getTabNameContainer($appendable=false)
  {
    $this->assertFetchMode();
  	return StringUtil::appendableString($this->getVarName().'.getNameContainer()',$appendable);
  }
  
}